<?php
require("bases.php");

class users extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$this->fmenu = $this->controller = "users";
			$this->position_one = array("text"=>"会员管理");
		}catch(Exception $e){

		}
	}

	public function datalist(){
		$obj_user = spClass('user');
		$postdata = $this->spArgs();
		$page = intval(max($postdata['page'], 1));
		$condition = "1";
		if($searchkeys = $postdata['searchkeys']){
			$condition .= " and (hnyn_users.regemail like '%{$searchkeys}%' or hnyn_users.regtel like '%{$searchkeys}%' or hnyn_users.realname  like '%{$searchkeys}%')";
			$this->searchkeys = $searchkeys;
		}
		if($typeid = intval($postdata['typeid'])){
			$condition .= " and hnyn_users.typeid = $typeid";
			$this->typeid = $typeid;
		}
		if($postdata['status'].'a' !== 'a'){
			$condition .= " and hnyn_users.status = ".intval($postdata['status']);
			$this->status = intval($postdata['status']);
		}
		$this->data_rs = $obj_user
			->join("hnyn_users_type", "hnyn_users_type.id = hnyn_users.typeid")
			->spPager($page, 10)->findAll($condition,  'hnyn_users.regtime desc', "hnyn_users.*, hnyn_users_type.name as typename");
		$this->type_rs = spClass("user_type")->getlist();
		$this->pager = $obj_user->spPager()->getPager();
		$this->url = spUrl('users', 'datalist', array("searchkeys"=>$this->searchkeys, "typeid"=>$this->typeid, "status"=>$this->status));
		$this->position_two = array("text"=>"会员管理", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"会员管理");
		$this->searchform = 1;
	}

	public function create(){
		$obj_user = spClass('user');
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			try{
				$postdate = $this->spArgs();
				$data = array();
				//客户端提交方法
				$data['realname'] = $postdate['realname'];
				$data['sex'] = intval($postdate['sex']);
				$data['birth'] = date("Y-m-d", strtotime($postdate['birth']));
				$data['regemail'] = $postdate['regemail'];
				$data['regtel'] = $postdate['regtel'];
				$data['password'] = $postdate['password'];
				$data['repassword'] = $postdate['repassword'];
				$data['status'] = $postdate['status'];
				$data['address'] = $postdate['address'];
				$data['regtime'] = strtotime($postdate["regtime"]);
				if($result = $obj_user->spValidator($data)) {
					foreach($result as $item) {
						throw new Exception($item[0]);
					}
				}
				if(!$obj_user->checkEmail($data["regemail"]))
					throw new Exception("该邮箱已被注册");
				if(!$obj_user->checkTel($data["regtel"]))
					throw new Exception("该联系电话已被注册");
				unset($data['repassword']);
				$data['password'] = md5($data['password']);
				if(!$obj_user->create($data))
					throw new Exception("未知错误，会员添加失败");
				$url = spUrl($this->controller, "datalist");
				$message = array('msg'=>'会员添加成功！','result'=>1, 'url'=>$url);
				echo json_encode($message);
				exit();
			}catch(Exception $e){
				$message = array('msg'=>$e->getMessage() ,'result'=>-1);
				echo json_encode($message);
				exit();
			}
		}
		$this->validator = $obj_user->getValidatorJS();
		$this->saveurl = spUrl("users", "create");
		$this->position_two = array("text"=>"会员添加", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"会员添加");
	}

	function modify(){
		try {
			$postdata = $this->spArgs();
			$obj_user = spClass("user");
			if(!$id = intval($postdata['id']))
				throw new Exception("非法操作");
			if(!$user_rs = $obj_user->find(array("id"=>$id)))
				throw new Exception("找不到该账号，可能已被删除");
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					$data = array();
					//客户端提交方法
					$data['realname'] = $postdata['realname'];
					$data['sex'] = intval($postdata['sex']);
					$data['birth'] = date("Y-m-d", strtotime($postdata['birth']));
					$data['regemail'] = $postdata['regemail'];
					$data['regtel'] = $postdata['regtel'];
					$data['password'] = $postdata['password'];
					$data['repassword'] = $postdata['repassword'];
					$data['status'] = $postdata['status'];
					$data['address'] = $postdata['address'];
					if($postdata["regtime"])
						$data['regtime'] = strtotime($postdata["regtime"]);
					if($result = $obj_user->spValidatoredit()->spValidator($data)) {
						foreach($result as $item) {
							throw new Exception($item[0]);
						}
					}
					if(!$obj_user->checkEmail($data["regemail"], $id))
						throw new Exception("该邮箱已被注册");
					if(!$obj_user->checkTel($data["regtel"], $id))
						throw new Exception("该联系电话已被注册");
					unset($data['repassword']);
					if($postdata['password']){
						$data['password'] = md5($data['password']);
					}else{
						unset($data['password']);
					}
					if(!$obj_user->update(array("id"=>$id), $data))
						throw new Exception("未知错误，用户更新失败");
					$url = spUrl("users", "datalist");
					$message = array('msg'=>'修改成功！','result'=>0, 'url'=>$url);
					echo json_encode($message);
					exit();
				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
		}catch(Exception $e){
			$this->redirect(spUrl($this->controller, "datalist"), "非法操作！");
		}
		$this->id = $id;
		$this->validator = $obj_user->getValidatoreditJS($id);
		$this->user_rs = $user_rs;
		$this->saveurl = spUrl($this->controller, "modify");
		$this->position_two = array("text"=>"会员修改", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"会员修改");
		$this->display($this->controller."/create.html");
	}

	public function delete(){
		$id = $this->spArgs('id');
		if($id){
			$user = spClass('user');
			$url = spUrl($this->controller, "datalist");
			if(is_array($id)){
				foreach ($id as $v){
					$conditions = array('id' => $v);
					$user->delete($conditions);
					//$user->update($conditions, $newrow);
				}
				echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
				exit();
			}
			else {
				$conditions = array('id' => $id);
				if($user->delete($conditions)){
					echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
					exit();
				}
				echo json_encode(array('msg'=>'删除失败！' , 'result'=>0, 'url'=>$url));
				exit();
			}
		}else{
			echo json_encode(array('msg'=>'请先选择删除项，再进行操作！' , 'result'=>0));
			exit();
		}
	}
	
	function usersdemo(){
		die("aa");
		$obj_demo = spClass('user_demo');
		$count = 1;
		$date = "2016-3-2";
		import("Rnd_China_Name.php");
		$obj_name = new Rnd_China_Name();
		for($i=0;$i<$count;$i++){
			$data = array();
			$data["realname"] = $obj_name->getName();
			$data["password"] = "96e79218965eb72c92a549dd5a330112";
			$data["regtel"] = $obj_demo->getRndTel();
			$data["regemail"] = $obj_demo->getRndMail($data);
			$data["sex"] = rand(0,2);
			$data["status"] = 1;
			$data["regtime"] = $obj_demo->getRandTime($date);
			if($id = $obj_demo->create($data))
				echo "create Success, id:$id <br/>";
			else
				echo "create Failed <br/>";
		}
	}
	
	function checkUsername(){
		@header('Cache-Control: no-cache, must-revalidate, max-age=0');
		@header('Pragma: no-cache');
		$user = spClass('user');
		echo $user->checkUsername($this->spArgs('username'), $this->spArgs('id'))?'true':'false';
		exit();
	}
	
	function modifystatus(){
		$id = $this->spArgs('id');
		$status = intval($this->spArgs("status"));
		$newrow = array("user_status"=>$status);
		if($id){
			$user = spClass('user');
			$url = spUrl("users", "userslist");
			if(is_array($id)){
				foreach ($id as $v){
					$conditions = array('uid' => $v);
					//$user->delete($conditions);
					$user->update($conditions, $newrow);
				}
				echo json_encode(array('msg'=>'更新成功！' , 'result'=>0, 'url'=>$url));
				exit();
			}
			else {
				$conditions = array('uid' => $id);
				if($user->update($conditions, $newrow)){
					echo json_encode(array('msg'=>'更新成功！' , 'result'=>0, 'url'=>$url));
					exit();
				}
				echo json_encode(array('msg'=>'更新失败！' , 'result'=>-1, 'url'=>$url));
				exit();
			}
		}else{
			echo json_encode(array('msg'=>'请先选择更新项，再进行操作！' , 'result'=>-2));
			exit();
		}
	}
}
?>